//
//  GPSTrainingMapDataView.swift
//  DaRings
//
//  Created by 尹琼 on 2022/12/9.
//

import UIKit
import MYModule
import RxCocoa
import RxSwift

open class GPSTrainingMapInfoView: View {
    
    open var type: TrainingType = .gps_Run {
        
        didSet {
            pauseBtn.backgroundColor = type.tintColor
            continueBtn.backgroundColor = type.tintColor
        }
        
    }
    
    open var infoModel: GPSTrainingInfoModel? {
        
        didSet {
            
            reloadData()
        }
        
    }
    
    public let dataView = TargetDataView()
  
    public let progressView = TrainingProgressView()
    
    public let trainingActionPublishSubject = PublishSubject<TrainingAction>()
    
    public let tap = PublishSubject<TrainingAction>()
    
    open override func makeUI() {
        super.makeUI()
        
        backgroundColor = UIColor.assist_FFFFFF
        
        collectionView.register(GPSTrainingMapItemCell.self, forCellWithReuseIdentifier: GPSTrainingMapItemCell.reuseIdentifier)
        collectionView.dataSource = self
        
        progressView.progressView.backgroundColor = UIColor.theme
        progressView.highProgressView.backgroundColor = UIColor.theme.withAlphaComponent(0.6)
        
        
        addSubview(dataView)
        addSubview(progressView)
        addSubview(collectionView)
        
        addSubview(lockBtn)
        addSubview(pauseBtn)
        addSubview(unlockBtn)
        addSubview(finishBtn)
        addSubview(continueBtn)
        
        dataView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(30)
        }
        
        progressView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.top.equalTo(dataView.snp.bottom).offset(10)
            make.height.equalTo(8)
        }
        
        collectionView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(progressView.snp.bottom).offset(28)
            make.height.equalTo(40)
        }
        
        unlockBtn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(52.5)
            make.right.equalToSuperview().offset(-52.5)
            make.top.equalTo(collectionView.snp.bottom).offset(32)
            make.height.equalTo(44)
            
        }
        
        pauseBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-37)
            make.left.equalTo(self.snp.centerX).offset(10)
            make.centerY.equalTo(unlockBtn.snp.centerY)
            make.height.equalTo(unlockBtn.snp.height)
        }
        
        lockBtn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(37)
            make.right.equalTo(self.snp.centerX).offset(-10)
            make.centerY.equalTo(unlockBtn.snp.centerY)
            make.height.equalTo(unlockBtn.snp.height)
        }
        
        
        finishBtn.snp.makeConstraints { make in
            make.edges.equalTo(lockBtn)
        }
        
        continueBtn.snp.makeConstraints { make in
            make.edges.equalTo(pauseBtn)
        }
        
        bind()
    }
    
    open func bind() {
        
        trainingActionPublishSubject.map { $0 != .training }.bind(to: lockBtn.rx.isHidden).disposed(by: rx.disposeBag)
        
        trainingActionPublishSubject.map { $0 != .training }.bind(to: pauseBtn.rx.isHidden).disposed(by: rx.disposeBag)
        
        trainingActionPublishSubject.map { $0 != .locked }.bind(to: unlockBtn.rx.isHidden).disposed(by: rx.disposeBag)
        
        trainingActionPublishSubject.map { $0 != .pause && $0 != .endTraining }.bind(to: finishBtn.rx.isHidden).disposed(by: rx.disposeBag)
        
        trainingActionPublishSubject.map { $0 != .pause && $0 != .endTraining }.bind(to: continueBtn.rx.isHidden).disposed(by: rx.disposeBag)
        
        trainingActionPublishSubject.map{ $0 }.bind(to: tap).disposed(by: rx.disposeBag)
        
        lockBtn.rx.tap.subscribe(onNext: {[weak self] in
            
            self?.trainingActionPublishSubject.onNext(.locked)
            
        }).disposed(by: rx.disposeBag)
        
        pauseBtn.rx.tap.subscribe(onNext: {[weak self] in

            self?.trainingActionPublishSubject.onNext(.pause)
            
        }).disposed(by: rx.disposeBag)
        
        finishBtn.rx.tap.subscribe(onNext: {[weak self] in

            self?.trainingActionPublishSubject.onNext(.endTraining)
            
        }).disposed(by: rx.disposeBag)
        
        continueBtn.rx.tap.subscribe(onNext: {[weak self] in

            self?.trainingActionPublishSubject.onNext(.training)
            
        }).disposed(by: rx.disposeBag)
        
        
        unlockBtn.actionWithClosure { [weak self] (state) in
            
            switch state {
        
            case .End:
                
                self?.trainingActionPublishSubject.onNext(.training)
                break
            default:
                break
                
            }
        }
        
        trainingActionPublishSubject.onNext(.training)
        
    }
    
    
    open func reloadData() {
        
        
        dataView.dataLabel.text = infoModel?.targetItem.currentDataTitle
        
        var str = "/"
        if let targetDataTitle = infoModel?.targetItem.targetDataTitle {
            dataView.targetLabel.text = "\(str)\(targetDataTitle)"
            str = ""
        } else {
            str = ""
            dataView.targetLabel.text = nil
        }
        if !str.isEmpty, let unit = infoModel?.targetItem.unit {
            dataView.unitLabel.text = "\(str)\(unit)"
        } else {
            dataView.unitLabel.text = infoModel?.targetItem.unit
        }
        
        progressView.progress = infoModel?.targetItem.progress
        progressView.highProgress = infoModel?.targetItem.highProgress
        if let targetData = infoModel?.targetItem.targetData, targetData > 0 {
            progressView.isHidden = false
        } else {
            progressView.isHidden = true
        }
       
        collectionView.reloadData()
    }
    
   
    public lazy var collectionView: CollectionView = {
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: UIScreen.main.bounds.size.width/4.0, height: 40)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        layout.scrollDirection = .horizontal
        
        let collectionView = CollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.isScrollEnabled = false
        return collectionView
        
    }()
    
    public let lockBtn: Button = {
        let btn = Button()
        btn.layer.borderColor = UIColor.assist_D0D0D0.cgColor
        btn.layer.borderWidth = 1
        setShadow(view: btn,sColor: .clear, cornerRadius: 22)
        btn.titleLabel?.font = Fonts.systemFont(ofSize: 18)
        btn.setTitleColor(.lightGray, for: .normal)
        btn.setTitle("gps_training_lock".language, for: .normal)
        return btn
    }()
    
    public let pauseBtn: Button = {
        let btn = Button()
        btn.cornerRadius = 22
        btn.titleLabel?.font = Fonts.systemFont(ofSize: 18)
        btn.setTitleColor(.white, for: .normal)
        btn.setTitle("gps_training_pause".language, for: .normal)
        return btn
    }()
    
    public let unlockBtn: GPSTrainingLDPressButton = {
        let btn = GPSTrainingLDPressButton()
        btn.layer.cornerRadius = 24
        btn.layer.masksToBounds = true
        btn.setTitleColor(.lightGray, for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 18)
        btn.backgroundColor = .clear
        btn.setTitle("gps_training_unlock".language, for: .normal)
        btn.layer.borderColor = UIColor.assist_D0D0D0.cgColor
        btn.layer.borderWidth = 1
        return btn
    }()
    
    public let finishBtn: Button = {
        let btn = Button()
        btn.cornerRadius = 22
        btn.titleLabel?.font = Fonts.systemFont(ofSize: 18)
        btn.backgroundColor = UIColor.GPSTraining.finish
        btn.setTitleColor(UIColor.assist_FFFFFF, for: .normal)
        btn.setTitle("gps_training_finish".language, for: .normal)
        return btn
    }()
    
    public let continueBtn: Button = {
        let btn = Button()
        btn.cornerRadius = 22
        btn.titleLabel?.font = Fonts.systemFont(ofSize: 18)
        btn.setTitleColor(UIColor.assist_FFFFFF, for: .normal)
        
        btn.setTitle("gps_training_continue".language, for: .normal)

        return btn
    }()

}

extension GPSTrainingMapInfoView {
    
    open class TargetDataView: View {
        
        public let dataLabel = Label()
        public let targetLabel = Label()
        public let unitLabel = Label()
        
        open override func makeUI() {
            super.makeUI()
            
            dataLabel.textColor = UIColor.assist_222222
            dataLabel.font = Fonts.boldSystemFont(ofSize: 40)
            
            targetLabel.textColor = UIColor.assist_222222
            targetLabel.font = Fonts.systemFont(ofSize: 20)
            
            addSubview(dataLabel)
            addSubview(targetLabel)
            addSubview(unitLabel)
            
            dataLabel.snp.makeConstraints { make in
                make.left.equalToSuperview()
                make.centerY.equalToSuperview()
                make.bottom.equalToSuperview()
            }
            
            targetLabel.snp.makeConstraints { make in
                make.left.equalTo(dataLabel.snp.right)
                make.centerY.equalToSuperview()
            }
            
            unitLabel.snp.makeConstraints { make in
                make.left.equalTo(targetLabel.snp.right)
                make.centerY.equalToSuperview()
                make.right.equalToSuperview()
            }
            
        }
        
    }
    
}

extension GPSTrainingMapInfoView: UICollectionViewDataSource {
    
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return infoModel?.items.count ?? 0
    }
    
    
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: GPSTrainingMapItemCell.reuseIdentifier, for: indexPath) as! GPSTrainingMapItemCell
        
        cell.model = infoModel?.items[indexPath.item]
        
        return cell
    }
    
}

extension GPSTrainingMapInfoView {
    
    open class GPSTrainingMapItemCell: CollectionViewCell {
        
        open var model: GPSTrainingInfoItemModel? {
            
            didSet {
                
                dataLabel.text = model?.currentDataTitle
                titleLabel.text = model?.title
                self.setupImage(imageName: model?.imageName ?? "")
            }
        }
        
        public let icon = ImageView()
        
        public let dataLabel = Label()
        
        public let titleLabel = Label()
        
        open override func makeUI() {
            super.makeUI()
            
            dataLabel.font = Fonts.systemFont(ofSize: 16)
            dataLabel.textColor = UIColor.assist_222222
            
            titleLabel.font = Fonts.systemFont(ofSize: 12)
            titleLabel.textColor = UIColor.assist_D0D0D0
            
            contentView.addSubview(dataLabel)
            contentView.addSubview(titleLabel)
            contentView.addSubview(self.icon)

            dataLabel.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalToSuperview()
            }
            
            icon.snp.makeConstraints { make in
                make.trailing.equalTo(dataLabel.snp.leading).offset(-3)
                make.centerY.equalTo(dataLabel.snp.centerY)
            }
            
            titleLabel.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalTo(dataLabel.snp.bottom).offset(5)
                
            }
            
        }
        
        public func setupImage(imageName: String) {
            dataLabel.snp.remakeConstraints { make in
                if imageName.isEmpty {
                    make.centerX.equalToSuperview()
                } else {
                    make.centerX.equalToSuperview().offset(14)
                }
                
                make.top.equalToSuperview()
            }
            icon.image = UIImage(named: imageName)
        }
        
    }
    
}

extension GPSTrainingMapInfoView {
    
    public enum TrainingAction {
        
        case pause
        case locked
        case training
        case endTraining
    }
    
}
